<?php

namespace App;

use DB;
use Illuminate\Database\Eloquent\Model;
use Storage;
use Hash;
/**
 * Class HealthyNum
 * @package App
 */
class HealthyNum extends Model
{
    public $timestamps = false;
    protected $table = 'kl_healthy_num';

    const DEL_ON = 0;
    const DEL_OFF = 1;

    static $DelDisplay = [
        self::DEL_ON => '删除',
        self::DEL_OFF => '未删除', 
    ];

    static function PostingsUsers()
    {
        return DB::table('kl_healthy_num')->select('id', 'name', 'healthy_num_name')->orderByDesc('id')->where('is_del', self::DEL_OFF)->get()->toArray();
    }
    /**
     * 添加健康号
     * @param $request
     * @return array
     */
    static function JkangPublish($request)
    {
        $jkang = new HealthyNum();

        if ($request->has('pic_path')) {
            $path = Storage::url(Postings::OssUpdate($request->pic_path));
            $jkang->pic_path = $path;
        } else {
            return ['status' => 2, 'message' => '图片呢?'];
        }
        if (intval($request->is_del) === self::DEL_ON) {
            $jkang->is_del = self::DEL_ON;
        } else {
            $jkang->is_del = self::DEL_OFF;
        }
		
        // 姓名
        if ($request->has('name')) {
            $jkang->name = $request->name;
        }
        // 密码
        if ($request->has('pass_word')) {
           $jkang->pass_word = bcrypt($request->pass_word);
        }		

        $jkang->healthy_num_name = $request->healthy_num_name;
		$jkang->create_time = date("Y-m-d H:i:s",time());
		$jkang->user_id = 1;
		$jkang->type = 1;
        if (!$jkang->save()) {
            return ['status' => 2, 'message' => '添加失败'];
        }

        return ['status' => 1, 'message' => '添加成功'];
    }


    /**
     * 修改健康号
     * @param $request
     * @param $HealthyNum
     * @return array
     */
    static function JkModify($request, $HealthyNum)
    {

        if ($request->has('pic_path')) {
            $path = Storage::url(Postings::OssUpdate($request->pic_path));
            $HealthyNum->pic_path = $path;
        }

        if (intval($request->is_del) === self::DEL_ON) {
            $HealthyNum->is_del = self::DEL_ON;
        } else {
            $HealthyNum->is_del = self::DEL_OFF;
        }

        // 姓名
        if ($request->has('name')) {
            $HealthyNum->name = $request->name;
        }
        // 密码
        if ($request->has('pass_word')) {
           $HealthyNum->pass_word = bcrypt($request->pass_word);
        }		

        $HealthyNum->healthy_num_name = $request->healthy_num_name;
		$HealthyNum->update_time = date("Y-m-d H:i:s",time());
		$HealthyNum->user_id = 1;
		$HealthyNum->type = 1;
        if (!$HealthyNum->save()) {
            return ['status' => 2, 'message' => '修改失败'];
        }

        return ['status' => 1, 'message' => '修改成功'];
    }
	
	//删除健康号
    static function Del($id, $request)
    {
        DB::beginTransaction();

        if (!HealthyNum::where('id', $id)->update([
            'is_del' => HealthyNum::DEL_ON
        ])) {
            DB::rollBack();

            return ['status' => 2, 'message' => '删除失败'];
        }

        DB::commit();

        return ['status' => 1, 'message' => '删除成功'];
    }
}

